//-----------------------------------------------------------------------
// <copyright file="AddUser.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation.  All rights reserved.
// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY 
// KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
// PARTICULAR PURPOSE.
// </copyright>
// <author>Andy Reeves</author>
// <email>andyr</email>
// <date>2004-03-23</date>
// <summary></summary>
//-----------------------------------------------------------------------

namespace Microsoft.Sdc.Tasks.Sql.Role
{
    using System;
    using System.Xml;
    using System.Globalization;
    using System.IO;
    using System.Data;
    using System.Data.SqlClient;
    using SdcSql = Microsoft.Sdc.Tasks.Configuration.SqlServer;
    using Microsoft.Build.Framework;

    /// <summary>
    /// Adds a login to the specified SQL role
    /// </summary>
    /// <remarks>
    /// <code><![CDATA[<Sql.Role.AddUser UserName="userName" ServerName="serverName" DatabaseName="database" roleName="role" />]]></code>
    /// <para>where:</para>
    /// <para><i>userName (Required)</i></para>
    /// <para>The login name to grant access to.</para>
    /// <para><i>databaseName (Required)</i></para>
    /// <para>The database containing the specified role.</para>
    /// <para><i>roleName (Required)</i></para>
    /// <para>The role to assign the user to.</para>
    /// <para><i>ConnectionString</i></para>
    /// <para>A valid connection string used to connect the SQL server to run the operation on.</para>
    /// <para><i>ServerName</i></para>
    /// <para>The server to connect to (used instead of <see cref="SqlBase.ConnectionString"/>)</para>
    /// <para><i>ConnectionTimeout</i></para>
    /// <para>Number of seconds to wait before failing a connection attempt. 
    /// Used if a <see cref="SqlBase.ConnectionString"/> is not specified.</para>
    /// <para><i>CommandTimeout</i></para>
    /// <para>Number of seconds to wait before failing the role request. </para>
    /// </remarks>
    /// <example>
    /// <code><![CDATA[
    /// <Project>
    ///     <Target Name="Test" >
    ///         <Sql.Role.AddUser
    ///             UserName="testUser"
    ///             ServerName="(local)"
    ///             DatabaseName="testDB"
    ///             RoleName="myRole" />
    ///     </Target>
    /// </Project>
    /// ]]></code>    
    /// </example>
    public class AddUser : SqlBase
    {
        private string databaseName = String.Empty;
        private string roleName = String.Empty;
        private string userName = String.Empty;

        /// <summary>
        /// Initializes a new instance of the AddSqlUserToRole class.
        /// </summary>
        public AddUser()
        {
        }

        /// <summary>
        /// Performs the action of this task.
        /// </summary>
        protected override void InternalExecute()
        {
            #region Check Parameters
            if (this.ConnectionString.Length == 0)
            {
                throw (new ApplicationException("No connection information set."));
            }
            #endregion

            #region Execute code

            Log.LogMessageFromResources("Sql.AddSqlUserToRole", userName, roleName);
            SdcSql.User.AddToRoleWithConnectionString(this.userName,
                this.roleName, this.databaseName, this.ConnectionString);

            #endregion
        }

        /// <summary>
        /// Database to perform the action on
        /// </summary>
        /// <value>A valid database name</value>
        [Required]
        public string DatabaseName
        {
            get
            {
                return (databaseName == null ? String.Empty : databaseName);
            }
            set
            {
                databaseName = value;

                return;
            }
        }
        
        /// <summary>
        /// Role to add login into
        /// </summary>
        /// <value>A valid SQL role</value>
        [Required]
        public string RoleName
        {
            get
            {
                return (roleName == null ? String.Empty : roleName);
            }
            set
            {
                roleName = value;

                return;
            }
        }

        /// <summary>
        /// Login account to admit to the specified role
        /// </summary>
        /// <value>A valid login account, in the form machine\username</value>
        [Required]
        public string UserName
        {
            get
            {
                return (userName == null ? String.Empty : userName);
            }
            set
            {
                userName = value;

                return;
            }
        }
    }
}